package com.example.sa.myapp.shopping_cart;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sa.myapp.R;

import java.util.List;

import static android.content.ContentValues.TAG;

public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.ViewHolder> {

    private List<Shop> mShopList;
    private Context context;
    private AmountView mAmountView;

    private static boolean[] checks;
    private static int length;

    static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView mBrand;
        private ImageView imageView;
        private TextView mName;
        private TextView mPrice;
        private TextView mNum;
        //定义数组来保存CheckBox的状态，必须定义为static才能在主活动中调用
        private static boolean[] checks;
        private static CheckBox mSel;

        ViewHolder(View view) {
            super(view);
            mBrand = (TextView) view.findViewById(R.id.tv_shop_car_head);
            imageView = (ImageView) view.findViewById(R.id.iv_shop_car_pro);
            mName = (TextView) view.findViewById(R.id.tv_shop_car_name);
            mPrice = (TextView) view.findViewById(R.id.tv_shop_car_price);
            mNum = (TextView) view.findViewById(R.id.etAmount);
            mSel = (CheckBox) view.findViewById(R.id.cb_shop_car_sel);
        }
    }



    public ShopAdapter(Context context, List<Shop> usersList) {
        this.context = context;
        this.mShopList = usersList;
        length = mShopList.size();
        checks = new boolean[length];//初始化数组
    }

    public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.shopp_car_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);

        mAmountView = (AmountView) view.findViewById(R.id.amount_view);
        mAmountView.setGoods_storage(50);

        holder.mSel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击按钮
                Log.d(TAG, "onClick: 单选");
            }
        });
        
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击头像事件
                Log.d(TAG, "onClick: 有效");
            }
        });
        return holder;
    }

    public static void setAllCheckBox(boolean ischeckbox) {
        allSelect(ischeckbox);
    }

    //增加这个函数，如果主活动的选中全选时，将listview中每个CheckBox的状态都改为选中就行
    public static void allSelect(boolean ischecked)
    {
        for(int i=0;i<length;i++)
        {
            checks[i]=ischecked;
            ViewHolder.mSel.setChecked(checks[i]);
        }
    }

    public void onBindViewHolder(final ViewHolder holder, final int position) {
        Shop shop = mShopList.get(position);
        holder.mBrand.setText(shop.getBrand());
        holder.imageView.setImageResource(shop.getImageUrl());
        holder.mName.setText(shop.getName());
        holder.mPrice.setText(shop.getPrice() + "￥");
        holder.mNum.setText(String.valueOf(shop.getNum()));
    }

    public int getItemCount() {
        return mShopList.size();
    }
}

